// index.js
import Dialog from '@vant/weapp/dialog/dialog';
const app = getApp()

Page({
    data:{
        item: {
            image: 'https://ts1.tc.mm.bing.net/th/id/R-C.5ef59f0c0a42fc32596ff2b5ecb1129a?rik=FdeV71lmW0LxKw&riu=http%3a%2f%2fstatic-data.gaokao.cn%2fupload%2fschool%2f202009%2f1607477450_5467_thumb.jpg&ehk=VV3doq8Jf%2bEKgI2wP622r%2fGuqzW1VYLPA6v40FZtwL0%3d&risl=&pid=ImgRaw&r=0',
            title: '示例标题',
            summary: '这是一段动态内容的摘要展示，支持多行显示，内容简洁明了……',
            time: '2025-04-25',
            tag: '热门'
        },
        activeIndex: 0,
        categoryList: [],
        newsList: [],
        currentPage: 1,
        pageSize: 10,
        hasMore: true,
        loading: false,
        latestAnnouncement: ''
    },

    onLoad() {
        this.loadCategories()
        this.loadNews()
        this.getAncement()
    },

    // 加载新闻分类
    loadCategories() {
        wx.request({
            url: `${app.globalData.baseUrl}/api/newsclass/list`,
            method: 'GET',
            success: (res) => {
                console.log(res);
                if (res.data.code === 200) {
                    this.setData({
                        categoryList: res.data.data
                    })
                }
            }
        })
    },

    // 加载新闻列表
    loadNews(refresh = false) {
        if (this.data.loading || (!refresh && !this.data.hasMore)) return

        this.setData({ loading: true })
        const { currentPage, pageSize, activeIndex, categoryList } = this.data
        const categoryId = categoryList[activeIndex]?.id

        wx.request({
            url: `${app.globalData.baseUrl}/api/news/list`,
            method: 'GET',
            data: {
                category_id: categoryId,
                page: currentPage,
                page_size: pageSize
            },
            success: (res) => {
                if (res.data.code === 200) {
                    const newList = res.data.data
                    this.setData({
                        newsList: refresh ? newList : [...this.data.newsList, ...newList],
                        hasMore: newList.length === pageSize,
                        currentPage: refresh ? 1 : currentPage + 1
                    })
                }
            },
            complete: () => {
                this.setData({ loading: false })
            }
        })
    },

    // 切换分类
    onCategoryTap(e) {
        const index = e.currentTarget.dataset.index
        this.setData({ 
            activeIndex: index,
            currentPage: 1,
            hasMore: true,
            newsList: []
        })
        this.loadNews(true)
    },

    // 查看新闻详情
    onNewsTap(e) {
        const id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: `/pages/newsdetail/newsdetail?id=${id}`
        })
    },

    // 下拉刷新
    onPullDownRefresh() {
        this.setData({
            currentPage: 1,
            hasMore: true,
            newsList: []
        })
        this.loadNews(true)
        wx.stopPullDownRefresh()
    },

    // 上拉加载更多
    onReachBottom() {
        this.loadNews()
    },

    Sqapply(){
        wx.navigateTo({
          url: '/pages/apply/apply'
        })
    },
    recordSec(){
        wx.navigateTo({
          url: '/pages/record/record',
        })
    },
    Bysheng(){
        wx.navigateTo({
          url: '/pages/schoolfellow/schoolfellow',
        })
    },
    NavToSchoolInfo(){
        wx.navigateTo({
          url: '/pages/schholIntroduce/schholIntroduce',
        })
    },
    navToApply(){
        wx.navigateTo({
          url: '/pages/schoolfellowapply/schoolfellowapply',
        })
    },
    navToReport(){
        wx.switchTab({
          url: '/pages/order/order',
        })
    },
    navToNewsList(){
        wx.navigateTo({
          url: '/pages/newslist/newslist',
        })
    },
    /* 公告获取 */
    getAncement(){
        wx.request({
          url: app.globalData.baseUrl+'/admin/querypub',
          method:"GET",
          success:(res)=>{
              console.log("最新",res);
              let list = res.data.data || res.data;
              if (Array.isArray(list) && list.length > 0) {
                  // 按time字段降序排序，取最新
                  list.sort((a, b) => new Date(b.time) - new Date(a.time));
                  const latest = list[0];
                  this.setData({
                      latestAnnouncement: latest.title
                  });
                  Dialog.alert({
                      title: '最新公告',
                      message: latest.title,
                  }).then(() => {
                      // on close
                  });
              }
          },fail:(res)=>{
              console.log(res)
          }
        })
    },
    navToGxfk(){
        wx.navigateTo({
          url: '/pages/historypub/historypub',
        })
    }
})
